﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using Spriter.Logic;
using OpenFileDialog = Microsoft.Win32.OpenFileDialog;

namespace Spriter
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly ImageBuilder _imageBuilder;
        private readonly ViewModel _viewModel;

        public MainWindow()
        {
            _viewModel = new ViewModel();
            DataContext = _viewModel;

            InitializeComponent();
            Init();

            _imageBuilder = new ImageBuilder();
        }

        private void PickAFile(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFileDialog {DefaultExt = ".png", Multiselect = true};

            bool? result = dialog.ShowDialog();

            if (result.Value)
            {
                _viewModel.Files = dialog.FileNames;
                FilesSelectedLabel.Visibility = Visibility.Visible;
            }
            ValidateForm();
        }

        private void DestinationClicked(object sender, RoutedEventArgs e)
        {
            var dialog = new FolderBrowserDialog();

            DialogResult result = dialog.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                _viewModel.FolderLocation = dialog.SelectedPath;
            }
            ValidateForm();
        }

        private async void Merge(object sender, RoutedEventArgs e)
        {
            await Dispatcher.InvokeAsync(() =>
            {
                Stream stream = _imageBuilder.GenerateSprite(333, 500, _viewModel.Files);

                string filename = Path.Combine(_viewModel.FolderLocation, _viewModel.Filename);
                filename += "." + FileTypeComboBox.SelectedValue;
                using (FileStream file = File.Create(filename))
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    stream.CopyTo(file);
                }
            });
        }

        private void Init()
        {
            FileTypeComboBox.ItemsSource = GetAllFileTypes().Select(i => i.ToString().ToLower());
            FileTypeComboBox.SelectedIndex = 0;

            FilesSelectedLabel.Visibility = Visibility.Collapsed;
            MergeButton.IsEnabled = false;
        }

        private IEnumerable<Constants.ImageType> GetAllFileTypes()
        {
            return Enum.GetValues(typeof (Constants.ImageType)).Cast<Constants.ImageType>();
        }

        private void ValidateForm()
        {
            MergeButton.IsEnabled = !string.IsNullOrEmpty(_viewModel.Filename) && _viewModel.Files.Any() &&
                                    !string.IsNullOrEmpty(_viewModel.FolderLocation) &&
                                    FileTypeComboBox.SelectedIndex >= 0;
        }

        private void FilenameChanged(object sender, TextChangedEventArgs e)
        {
            ValidateForm();
        }
    }
}